package com.yimixian.app.common;

import com.yimixian.app.cart.CartManager;
import com.yimixian.app.model.CartUpdate;
import com.yimixian.app.rest.api.CartUpdateAPI;

import org.json.JSONObject;

/**
 * Created by zhilian-2 on 2016/3/9.
 */
public class WebViewHttpActivity extends BaseHttpActivity{

    private CartUpdateAPI mCartUpdateAPI;

    /**
     * 发起添加或者删除商品到购物车请求
     * @param trend 1添加 0减少
     * @param goodId 商品id
     */
    public void onPlusOrMinusHttp(int trend, int goodId){
        if (!checkNetWorkState()){
            cartChangeResult(0);
            return;
        }
        if (mCartUpdateAPI == null) mCartUpdateAPI = new CartUpdateAPI(this);
        mCartUpdateAPI.mTrend = trend;
        mCartUpdateAPI.mGoodId = goodId;
        mCartUpdateAPI.getCartUpdate();
    }
    @Override
    public void initRequestAPI(String requestKeys) {

    }

    @Override
    public void fetchDataFromNetwork(String key) {

    }

    @Override
    protected String getTitleBarText() {
        return "";
    }

    @Override
    public void onHttpSuccess(String url, JSONObject responseJson) {
        if (url.contains(CartUpdateAPI.CART_UPDATE) && mCartUpdateAPI != null) {
            CartUpdate cartUpdate = mCartUpdateAPI.parseJson(responseJson);
            CartManager.getInstance().updateCartGoods(cartUpdate);
            cartChangeResult(1);
        }
    }

    @Override
    public void OnHttpFailure(String url, String errorMessage, boolean errorFromUs) {
        if (url.contains(CartUpdateAPI.CART_UPDATE)) {
            cartChangeResult(0);
        }
    }

    public void cartChangeResult(int status){
    }

}
